﻿(function () {
    "use strict";
    angular
        .module("codeVerificationSystem")
        .controller("TaskListController",
        [
            "taskResource",
            "$uibModal",
            "currentUser",
            "$window",
            "$http",
            TaskListController
        ]);

    function TaskListController(taskResource, $uibModal, currentUser, $window, $http) {
        var vm = this;

        if (!currentUser.getProfile().isLoggedIn) {
            $window.location.href = '/';
        }

        vm.currentUser = currentUser;
        vm.addTaskEnabled = false;
        
        vm.addTask = function () {
            $http({
                method: 'POST',
                url: 'http://localhost:49616/api/tasks',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                data: { Name: vm.taskToAdd.name, Description: vm.taskToAdd.description }
            }).then(function successCallback(response) {
                loadTasks();
                vm.addTaskEnabled = false;
                vm.taskToAdd = null;
            }, function errorCallback(response) {
                console.log("losho");
            });
        };

        vm.deleteTask = function (task) {
            $http({
                method: 'DELETE',
                url: 'http://localhost:49616/api/tasks',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                data: { Id: task.id }
            }).then(function successCallback(response) {
                loadTasks();
            }, function errorCallback(response) {
                console.log("losho");
            });
        };
        vm.viewSubmissions = function (task) {

            var modalInstance = $uibModal.open({
                animation: true,
                templateUrl: 'app/submissions/submissionListView.html',
                controller: 'SubmissionListController as vm',
                windowClass: "view-submissions-dialog",
                resolve: {
                    task: task
                }
            });

            modalInstance.result.then(function () {

            }, function () {

            });
        };

        vm.viewTests = function (task) {

            var modalInstance = $uibModal.open({
                animation: true,
                templateUrl: 'app/tests/testListView.html',
                controller: 'TestListController as vm',
                windowClass: "view-tests-dialog",
                resolve: {
                    task: task
                }
            });

            modalInstance.result.then(function () {

            }, function () {

            });
        };

        vm.openUpdateDialog = function(task) {

            var modalInstance = $uibModal.open({
                animation: true,
                templateUrl: 'app/submissions/submissionAddView.html',
                controller: 'SubmissionAddCtrl as vm',
                resolve: {
                    task: task
                }
            });

            modalInstance.result.then(function() {

            }, function() {

            });
        };

        vm.addTestDialog = function (task) {

            var modalInstance = $uibModal.open({
                animation: true,
                templateUrl: 'app/tests/testAddView.html',
                controller: 'TestAddCtrl as vm',
                resolve: {
                    task: task
                }
            });

            modalInstance.result.then(function () {

            }, function () {

            });
        };

        function loadTasks() {
            taskResource.query(function (data) {
                vm.tasks = data;
            });
        }

        loadTasks();
    }
}());
